package com.xjx.controller;

import com.xjx.common.PageResult;
import com.xjx.common.Result;
import com.xjx.pojo.dto.BookCreateDTO;
import com.xjx.pojo.dto.BookPageQuery;
import com.xjx.pojo.dto.BookUpdateDTO;
import com.xjx.pojo.dto.StockAdjustDTO;
import com.xjx.pojo.vo.BookVO;
import com.xjx.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/books")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    @PostMapping("/")
    public Result<BookVO> createBook(@Validated @RequestBody BookCreateDTO createDTO) {
        return Result.ok(bookService.createBook(createDTO));
    }

    @PutMapping("/{id}")
    public Result<BookVO> updateBook(@PathVariable Long id, @Validated @RequestBody BookUpdateDTO updateDTO) {
        return Result.ok(bookService.updateBook(id, updateDTO));
    }

    @PatchMapping("/{id}/stock/adjust")
    public Result<BookVO> adjustStock(@PathVariable Long id, @Validated @RequestBody StockAdjustDTO adjustDTO) {
        return Result.ok(bookService.adjustStock(id, adjustDTO));
    }

    @GetMapping("/{id}")
    public Result<BookVO> getBookById(@PathVariable Long id) {
        return Result.ok(bookService.getBookById(id));
    }

    @GetMapping("/exists/isbn/{isbn}")
    public Result<Map<String, Boolean>> checkIsbnExists(@PathVariable String isbn) {
        return Result.ok(bookService.checkIsbnExists(isbn));
    }

    @GetMapping("/page")
    public Result<PageResult<BookVO>> getBookPage(@Validated BookPageQuery query) {
        return Result.ok(bookService.queryBooksByPage(query));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteBook(@PathVariable Long id) {
        bookService.deleteBookById(id);
        return Result.ok();
    }

    @PutMapping("/{id}/restore")
    public Result<BookVO> restoreBook(@PathVariable Long id) {
        return Result.ok(bookService.restoreBookById(id));
    }
}